{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算图演示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "#################用户定义的数据#########################\n",
    "a = torch.eye(4,3)  #定义一个单位矩阵\n",
    "b = torch.ones_like(a)\n",
    "x = torch.add(a,1, b)  #矩阵相加\n",
    "w = torch.randn(len(x[0]) ,1, requires_grad=True )#定义一个随机矩阵\n",
    "\n",
    "##################计算生成数据######################\n",
    "y = torch.mm(x,w)   #矩阵乘法\n",
    "# y.requires_grad\n",
    "z = 0.2*y**2    \n",
    "m =(z-1)/2\n",
    "n =m-0.1\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查看用户定义的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a.is_leaf: True\n",
      "b.is_leaf: True\n",
      "x.is_leaf: True\n",
      "w.is_leaf: True\n"
     ]
    }
   ],
   "source": [
    "print('a.is_leaf:',a.is_leaf)\n",
    "print('b.is_leaf:',b.is_leaf)\n",
    "print('x.is_leaf:',x.is_leaf)\n",
    "print('w.is_leaf:',w.is_leaf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y.is_leaf: False\n",
      "z.is_leaf: False\n",
      "m.is_leaf: False\n",
      "n.is_leaf: False\n"
     ]
    }
   ],
   "source": [
    "print('y.is_leaf:',y.is_leaf)\n",
    "print('z.is_leaf:',z.is_leaf)\n",
    "print('m.is_leaf:',m.is_leaf)\n",
    "print('n.is_leaf:',n.is_leaf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a.requires_grad: False\n",
      "w.requires_grad: True\n",
      "y.requires_grad: True\n"
     ]
    }
   ],
   "source": [
    "print('a.requires_grad:',a.requires_grad)\n",
    "print('w.requires_grad:',w.requires_grad)\n",
    "print('y.requires_grad:',y.requires_grad)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 叶子节点无法计算出梯度\n",
    "\n",
    "p--->x------y\n",
    "\n",
    "          ---->g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x.grad: tensor([[2., 2., 2., 2.],\n",
      "        [2., 2., 2., 2.],\n",
      "        [2., 2., 2., 2.]])\n",
      "p.grad: tensor([[1.0806, 1.0806, 1.0806, 1.0806],\n",
      "        [1.0806, 1.0806, 1.0806, 1.0806],\n",
      "        [1.0806, 1.0806, 1.0806, 1.0806]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'1-gragh.ipynb\\ntorch.Size([3, 4])\\nx.grad: None\\np.grad: tensor([[0.5403, 0.5403, 0.5403, 0.5403],\\n        [0.5403, 0.5403, 0.5403, 0.5403],\\n        [0.5403, 0.5403, 0.5403, 0.5403]])\\n叶子节点的梯度是None，非叶子节点的梯度是有值的。\\n'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建一个张量\n",
    "p = torch.ones(3,4,requires_grad=True)\n",
    "x = torch.sin(p)\n",
    "y = x*2\n",
    "# print(p.shape)\n",
    "g = torch.ones_like(x)\n",
    "x.retain_grad()\n",
    "y.backward(g)\n",
    "print('x.grad:',x.grad)  #打印梯度\n",
    "print('p.grad:',p.grad)  #打印梯度\n",
    "'''1-gragh.ipynb\n",
    "torch.Size([3, 4])\n",
    "x.grad: None\n",
    "p.grad: tensor([[0.5403, 0.5403, 0.5403, 0.5403],\n",
    "        [0.5403, 0.5403, 0.5403, 0.5403],\n",
    "        [0.5403, 0.5403, 0.5403, 0.5403]])\n",
    "叶子节点的梯度是None，非叶子节点的梯度是有值的。节点计算梯度后会默认释放内存，无法再计算梯度。\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dl2024",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
